package com.tang.framework.config.mybatisPlus.interceptor;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.SQLException;

/**
 * @Author tang jun liang
 * @Date 2023/8/9 08:18
 */
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class CustomizeTenantLineInnerInterceptor extends TenantLineInnerInterceptor {


    public CustomizeTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }


    public CustomizeTenantLineInnerInterceptor() {
    }


    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        String sql = boundSql.getSql();
        String[] sqlSplit = sql.toLowerCase().split("from");
        if (sqlSplit[sqlSplit.length -1].contains(getTenantLineHandler().getTenantIdColumn())){
            return;
        }
        if (sql.contains("gen_field") || sql.contains("gen_field_type") || sql.contains("gen_table") || sql.contains("sys_dict_type") || sql.contains("sys_dict_value")){
            return;
        }
        super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }
}
